<?php  require "../../../global.php";
 
 $h=fopen("logs_".date("Y-m-d").".txt","a+");
 fwrite($h,"todo..".date("Y-m-d H:i:s")."\n ");
 $rr=implode("&",$_REQUEST);
 fwrite($h,"suss:arr to this：$rr ".date("Y-m-d H:i:s")."\n ");
   //从PayPal 出读取POST 信息同时添加变量 "cmd
    $req = 'cmd=_notify-validate'; 
   foreach ($_POST as $key => $value) { 
   $value = urlencode(stripslashes($value)); 
   $req .= "&$key=$value"; 
   } 
    //建议在此将接受到的信息记录到日志文件中以确认是否收到IPN 信息 
    //将信息POST 回给PayPal 进行验证 
  $header .= "POST /cgi-bin/webscr HTTP/1.0\r\n"; 
  $header .= "Content-Type:application/x-www-form-urlencoded\r\n"; 
  $header .= "Content-Length:" . strlen($req) ."\r\n\r\n"; 
  //在Sandbox 情况下，设置： 
  //$fp = fsockopen("www.sandbox.paypal.com‟,80,$errno,$errstr,30); 
  $fp = fsockopen ('www.paypal.com', 80, $errno, $errstr, 30); 
  //将POST 变量记录在本地变量中 
  //该付款明细所有变量可参考： 
  //https://www.paypal.com/IntegrationCenter/ic_ipn-pdt-variable-reference.html 
  $item_name = $_POST['item_name']; //产品名称
  $item_number = $_POST['item_number']; //数据
  $payment_status = $_POST['payment_status']; //状态
  $payment_amount = $_POST['mc_gross']; 
  $payment_currency = $_POST['mc_currency']; 
  $txn_id = $_POST['txn_id']; 
  $receiver_email = $_POST['receiver_email']; 
  $payer_email = $_POST['payer_email']; 
  //… 
  //判断回复POST 是否创建成功 
  if (!$fp) { 
   //HTTP  错误 
  }else { 
  //将回复POST 信息写入SOCKET 端口 
  fputs ($fp, $header .$req); 
  //开始接受PayPal 对回复POST 信息的认证信息 
  while (!feof($fp)) { 
  $res = fgets ($fp, 1024); 
  //已经通过认证 
  if (strcmp ($res, "VERIFIED") == 0) { 
  //检查付款状态 
  //检查txn_id  是否已经处理过 
  //检查receiver_email 是否是您的PayPal 账户中的EMAIL 地址 
  //检查付款金额和货币单位是否正确 
  //处理这次付款，包括写数据库
  			//若付款成功，写自己的数据库
			
			preg_match("/^[a-zA-Z][0-9]+/",$_REQUEST["invoice"],$orderAr);
			$invoicel=$orderAr[0];
			$payflag=str_replace($invoicel,"",$_REQUEST["invoice"]);
			
			
			
			require RootDir."/inc/dabase_mysql.php";
			$mydb=new YYBDB();
			$sql2="earnestmoney=".$payment_amount.",paytimeearnestmoney=".time().",paytype='paypal',earnbizhong='USD',isconfim=1";
			$paytype="dj";
				
			if($payflag!="")
			{	
				if($payflag=="je2")
				{
					$sql2="balance=".$payment_amount.",paytimebalance=".time().",balancepaytye='paypal',balancebizhong='USD',balanceisconfim=1";
					$paytype="je2";
				}
				if($payflag=="je3")
				{
					$sql2="je3=".$payment_amount.",je3paytime=".time().",je3paytype='paypal',je3bizhong='USD',je3isconfim=1";
					$paytype="je3";
				}
				if($payflag=="je4")
				{
					$sql2="je4=".$payment_amount.",je4paytime=".time().",je4paytype='paypal',je4bizhong='USD',je4isconfim=1";
					$paytype="je4";
				}
				
			}
			
			
			$sql="update ".$SystemConest[7]."orderpaylist set 
			".$sql2."
			 where orderno='".$invoicel."'";
			 $mydb->db_query($sql);
			
			 $res=$mydb->db_query("select * from ".$SystemConest[7]."tourorder where orderno='".$invoicel."'");
			 $rs=$mydb->db_fetch_array($res);
/* regin 支付成功，通知联盟系统 */
			 	require_once("../track.php");
				$params = array(
					"TotalCost"=>$payment_amount * 6.3,
					"OrderID"=>$invoicel,
					"data1"=>Marketools_GetTrackToken()
				);
				@Marketools_SaleNotify($params);
/* endregin 支付成功，通知联盟系统 */
			 //支付成功，执行发送邮件操作
			 echo "<iframe  src='/dszSendMail_sendMailInfo_type_paysuss_orderno_".$invoicel."_paytype_paypal_djorwk_".$paytype."_email_".$rs["nuseremail"]."' 
frameborder=0 scrolling=no width=1 height=1></iframe>";
?>
			<script src="http://union.dreams-travel.com/api/track_js.php" type="text/javascript"></script>
			<script id="pap_x2s6df8d" src="http://union.dreams-travel.com/scripts/sale.js" type="text/javascript"></script>
			<script type="text/javascript">
			<!--
				var TotalCost="<?php echo $payment_amount; ?>";
				var OrderID="<?php echo $invoicel; ?>";
				papSale();
			-->
			</script>
<?php
			 unset($rs);

  			fwrite($h,"updated the datebase!::$sql:".date("Y-m-d H:i:s")."\n ");
  
   
  }else if (strcmp ($res, "INVALID") == 0) { 
  //未通过认证，有可能是编码错误或非法的POST 信息 
  } 
  } 
  fclose ($fp); 
} 
fclose($h);